const path = require("path");
const glob = require("glob");
const isPro = process.env.NODE_ENV === "production";
// const publicPath = isPro ? '/login' : '/login'
const devtool = isPro ? false : "cheap-module-eval-source-map";

function resolve(dir) {
  return path.join(__dirname, dir);
}

// 配置pages多页面获取当前文件夹下的html和js
function getEntry(globPath) {
  let entries = {},
    tmp;
  glob.sync(globPath).forEach(function (entry) {
    tmp = entry.split("/").splice(-3);
    entries[tmp[1]] = {
      entry: "src/pages/" + tmp[1] + "/main.js",
      template: "public/" + tmp[1] + ".html",
      filename: tmp[1] + '.html'
    };
  });
  return entries;
}

let pages = getEntry("./src/pages/**?/login.vue");

module.exports = {
  publicPath: process.env.VUE_APP_BASEURL || "./",
  // 开发时启动eslint校验
  lintOnSave: !isPro,
  pages,
  productionSourceMap: false,
  configureWebpack() {
    return {
      devtool,
      resolve: {
        alias: {
          "@": resolve("src"),
          public: resolve("public"),
          config: resolve("config"),
        },
      },
    };
  },
  chainWebpack(config) {
    // 去掉元素间的空格
    config.module
      .rule("vue")
      .use("vue-loader")
      .loader("vue-loader")
      .tap((options) => {
        options.compilerOptions.preserveWhitespace = true;
        return options;
      })
      .end();
    // config.when(isPro, (config) => {
    //   config.optimization.splitChunks({
    //     chunks: "all",
    //     cacheGroups: {
    //       libs: {
    //         name: "chunk-libs",
    //         test: /[\\/]node_modules[\\/]/,
    //         priority: 10,
    //         chunks: "initial", // only package third parties that are initially dependent
    //       },
    //       commons: {
    //         name: "chunk-commons",
    //         test: resolve("src/components"), // can customize your rules
    //         minChunks: 3, //  minimum common number
    //         priority: 5,
    //         reuseExistingChunk: true,
    //       },
    //     },
    //   });
    //   config.optimization.runtimeChunk("single");
    // });
  },
  css: {
    loaderOptions: {
      sass: {
        prependData: `@import "~@/styles/variables/_variables.sass"`,
      },
      scss: {
        prependData: `@import "~@/styles/variables/_variables.scss";`,
      },
    },
  },
  devServer: {
    https: false,
    // index: 'default.html',
    port: 9091,
    // 启动eslint格式校验弹窗
    // overlay: {
    //   warnings: false,
    //   errors: true
    // },
    proxy: {
      [process.env.VUE_APP_OAUTH2_PATH]: {
        target: process.env.VUE_APP_OAUTH2_URL,
        ws: false,
        changeOrigin: true,
      },
    }
  }
}